/**
*      author: Y_Yao
*      created: 2025-02-15 15:09:24
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 7;
int n;
int fa[N],sz[N];

int find(int x)
{
    if(fa[x] == x) return x;
    else fa[x] = find(fa[x]);
    return fa[x];
}

void Union(int u,int v)
{
    u = find(u);
    v = find(v);
    if(u == v) return;

    if(sz[u] > sz[v]) swap(u,v);
    sz[v] += sz[u];
    sz[u] = 0;
    fa[u] = v;
}

bool luck(int x)
{
    while(x != 0)
    {
        int r = x % 10;
        if(r != 4 && r != 7)
        {
            return false;
        }
        x = x / 10;
    }
    return true;
}

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        fa[i] = i;
        sz[i] = 1;
    }
    for(int i = 1;i < n;i++)
    {
        int u,v,w;
        cin >> u >> v >> w;
        if(!luck(w)) Union(u,v);
    }

    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        if(sz[i])
        {
            ans += sz[i] * (n - sz[i]) * (n - sz[i] - 1);
        }
    }

    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}
